package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
//涉及多张表的操作，我们要开启事务
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    /**
     * 新增菜品的同时保存口味数据
     * @param dishDto
     */
    @Autowired
    private DishFlavorService dishFlavorService;
    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //1:新增菜品
        this.save(dishDto);
        //2:获取新增菜品的id
        Long id = dishDto.getId();
        //3:保存菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 通过id查询菜品信息和口味信息
     * @param id
     * @return
     */
    @Override
    public R<DishDto> selectDishwithFlavor(Long id) {
        //1：先查询菜品信息
        Dish dish = this.getById(id);
        //2:查询口味信息
        //2.1:创建构造器
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //2.2:创建条件
        queryWrapper.eq(DishFlavor::getDishId,id);
        //2.3:查询口味信息
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        //3:创建DishDto对象
        DishDto dto = new DishDto();
        //4:完成对象转换(属性赋值)
        BeanUtils.copyProperties(dish,dto);
        dto.setFlavors(list);
        return R.success(dto);
    }

    /**
     * 根据提交内容修改菜品信息或者口味信息
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public R<String> updateDishWithFlavor(DishDto dto) {
        //1;先把菜品信息和口味信息分离开("操作的是两张表")
        Dish dish = new Dish();
        DishFlavor dishFlavor = new DishFlavor();
        //2:完成数据封装(属性拷贝)
        BeanUtils.copyProperties(dto,dish);
        BeanUtils.copyProperties(dto,dishFlavor);
        //3:先对菜品信息进行修改
        this.updateById(dish);
        //4:对口味信息进行修改,清除当前菜品对应口味数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorService.removeById(queryWrapper);
        //5:添加当前提交过来的口味数据   (dto里面没有dish_id所以需要我们自己去封装)
        List<DishFlavor> list = new ArrayList<>();
        List<DishFlavor> flavors = dto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
            list.add(flavor);
        }
        dishFlavorService.saveBatch(list);
        return R.success("修改成功");
    }
}
